package com.xunk.equipment.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xunk.common.core.constant.HttpStatus;
import com.xunk.common.core.enums.SupplierEnum;
import com.xunk.common.core.exception.CustomException;
import com.xunk.common.core.utils.StringUtils;
import com.xunk.common.core.utils.XunkAssert;
import com.xunk.common.core.utils.sql.SqlUtil;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.common.core.web.page.PageDomain;
import com.xunk.common.core.web.page.TableDataInfo;
import com.xunk.common.core.web.page.TableSupport;
import com.xunk.common.redis.service.RedisService;
import com.xunk.equipment.context.RequestHeaderContext;
import com.xunk.equipment.domain.EquipmentDO;
import com.xunk.equipment.mapper.EquipmentMapper;
import com.xunk.equipment.service.BaseService;
import com.xunk.equipment.service.IEquipmentNotifyService;
import com.xunk.equipment.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;


public abstract class BaseController {

    @Autowired
    RedisService redisService;

    @Autowired
    EquipmentMapper equipmentMapper;

    @Autowired
    Map<String, IEquipmentNotifyService> equipmentNotifyMap = new ConcurrentHashMap<>();

    @Autowired
    ApplicationContext applicationContext;

    @Autowired
    BaseService baseService;


    /**
     * 根据deviceSn查询设备信息，缓存14天
     *
     * @param deviceSn
     * @return
     */
    protected EquipmentDO getEquipmentInfo(String deviceSn) {
        return baseService.getEquipmentInfo(deviceSn);
    }

    @SuppressWarnings("unchecked")
    public <T> T getEquipmentServiceHandler(EquipmentDO equipmentInfo) {
        String supplier = equipmentInfo.getSupplier();
        SupplierEnum supplierEnum = SupplierEnum.parseSupplierEnum(supplier);
        XunkAssert.isTrue(!Objects.equals(supplierEnum, SupplierEnum.UNKNOWN), "不支持的厂商设备 " + equipmentInfo.getDeviceSn());
        String deviceType = equipmentInfo.getDeviceType();
        return (T) applicationContext.getBean(supplier.concat("-").concat(deviceType), IEquipmentService.class);
    }

    /**
     * 获取service实现类
     *
     * @param supplier
     * @return
     */
    public IEquipmentNotifyService getEquipmentNotifyService(String supplier) {
        return equipmentNotifyMap.get(supplier);
    }

    /**
     * 获取token，对应的访问路径需要在WebConfig中增加拦截
     *
     * @return
     */
    public String getToken() {
        return RequestHeaderContext.getInstance().getToken();
    }

    /**
     * 获取userId，对应的访问路径需要在WebConfig中增加拦截
     *
     * @return
     */
    public String getUserId() {
        return RequestHeaderContext.getInstance().getUserId();
    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    protected AjaxResult getAjaxResult(List<?> list) {
        Map<String, Object> data = new HashMap<>();
        data.put("rows", list);
        data.put("total", new PageInfo(list).getTotal());
        return AjaxResult.success("查询成功", data);
    }
}
